package com.taotao.shop.product.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.taotao.shop.product.pojo.entity.PmsCategory;
import com.taotao.shop.product.mapper.PmsCategoryMapper;

import com.taotao.shop.product.pojo.entity.PmsCategoryBrandRelation;
import com.taotao.shop.product.service.PmsCategoryBrandRelationService;
import com.taotao.shop.product.service.PmsCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品三级分类 服务实现类
 * </p>
 *
 * @author zwr
 * @since 2021-06-14
 */
@Service
public class PmsCategoryServiceImpl extends ServiceImpl<PmsCategoryMapper, PmsCategory> implements PmsCategoryService {

    @Autowired
   private PmsCategoryBrandRelationService categoryBrandRelationService;

    /**
     * 查询商品分类-树形展示
     * @return
     */
    @Override
    public List<PmsCategory> listWithTree() {
        //1、查询出所有分类
        List<PmsCategory> entities = super.baseMapper.selectList(null);

        //2、组装成父子的树形结构

        //2.1)、找到所有一级分类
        List<PmsCategory> levelMenus = entities.stream()
                .filter(e -> e.getParentCid() == 0)
                .map((menu) -> {

                    menu.setChildren(getChildrens(menu, entities));
                    return menu;
                })
                .sorted((menu, menu2) -> {
                    return (menu.getSort() == null ? 0 : menu.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
                })
                .collect(Collectors.toList());

        return levelMenus;
    }


    /**
     * 级联更新所有关联的数据
     * @param category
     */
    @Transactional
    @Override
    public void updateCascade(PmsCategory category) {
        this.updateById(category);
        categoryBrandRelationService.updateCategory(category.getCatId(),category.getName());
    }



    @Override
    public void removeMenuByIds(List<Long> asList) {

        //TODO 检查当前删除的菜单，是否被别的地方引用
        List<PmsCategoryBrandRelation> categoryBrandRelation =
                categoryBrandRelationService.list(new QueryWrapper<PmsCategoryBrandRelation>().in("catelog_id", asList));

        if (categoryBrandRelation.size() == 0) {
            //逻辑删除
            baseMapper.deleteBatchIds(asList);
        } else {
            throw new RuntimeException("该菜单下面还有属性，无法删除!");
        }
    }


    /**
     * 递归查找所有菜单的子菜单
     * @param root
     * @param all
     * @return
     */
    private List<PmsCategory> getChildrens(PmsCategory root, List<PmsCategory> all) {

        List<PmsCategory> children = all.stream().filter(categoryEntity -> {
            return categoryEntity.getParentCid().equals(root.getCatId());
        }).map(categoryEntity -> {
            //1、找到子菜单(递归)
            categoryEntity.setChildren(getChildrens(categoryEntity, all));
            return categoryEntity;
        }).sorted((menu, menu2) -> {
            //2、菜单的排序
            return (menu.getSort() == null ? 0 : menu.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
        }).collect(Collectors.toList());

        return children;

    }
}
